/**
 * Title: MainActivity.java
 * Description:
 * author: kotomifi
 * date: Jul 6, 2014
 * ©2014 apping 社区
 */
package com.apping.demo.bbs;

import com.apping.demo.bbs.utils.SharedPreferencesUtil;
import com.sina.weibo.sdk.demo.R;
import com.umeng.update.UmengUpdateAgent;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

/**
 *
 */
public class MainActivity extends Activity {

	SharedPreferences preferences;
	SharedPreferences.Editor editor;
	
	private Intent m_intent;
	private Class<?> m_cls;
	
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//友盟自动更新
		UmengUpdateAgent.update(this);
		setContentView(R.layout.main);

		m_intent = new Intent();
		preferences = getSharedPreferences("count", MODE_WORLD_READABLE);
		
		// 读取程序打开次数
		int count = preferences.getInt("count", 0);
		if (0 == count) {
			setNextActivity(WelcomeActivity.class);
			nextActivity();
			finish();
		} else {
			// 判断udi是否有效
			SharedPreferencesUtil sp = new SharedPreferencesUtil(this);
			String uid = sp.readString("com_weibo_sdk_android", "uid");
			String access_token = sp.readString("com_weibo_sdk_android", "access_token");
			//String expires_in = sp.readString("com_weibo_sdk_android", "expires_in");
			//sp.writeString("com_weibo_sdk_android", "expires_in", "120");
			if (uid == null) {
				setNextActivity(WelcomeActivity.class);
				nextActivity();
				Log.i("TAG", "hello");
			} else {
				setNextActivity(ArticleActivity.class);
				nextActivity();
				finish();
			}
		}
		editor = preferences.edit();
		editor.putInt("count", ++count);
		editor.commit();
	}
	
	public void nextActivity() {
		m_intent.setClass(getApplication(), m_cls);
		startActivity(m_intent);
	}
	
	public void setNextActivity(Class<?> cls) {
		m_cls = cls;
	}
	
}
